<template>
    <el-form
        size="small"
        class="reStartForm"
        ref="reStartForm"
        :model="reStartForm"
        label-width="80px"
        :rules="reStartFormRules"
    >
        <el-form-item label="重启设置" prop="value">
            <el-select
                v-if="equipTypeCode == 'EC'"
                v-model="reStartForm.value"
                placeholder="请选择重启设置"
                class="restart-select"
            >
                <el-option
                    v-for="item in enums.EQUIP_RESTART_TYPE"
                    :key="item.code"
                    :label="item.label"
                    :value="item.code"
                >
                </el-option>
            </el-select>
            <el-select
                v-else-if="equipTypeCode == 'CS'"
                v-model="reStartForm.value"
                placeholder="请选择重启设置"
                class="restart-select"
            >
                <el-option
                    v-for="item in enums.EQUIP_RESTART_TYPE.filter(
                        (item) => item.code == 1
                    )"
                    :key="item.code"
                    :label="item.label"
                    :value="item.code"
                >
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="重启地址" prop="addrId">
            <el-checkbox
                v-model="reStartradio"
                label="全部"
                @change="reStartradioChange"
            ></el-checkbox>
            <el-input-number
                style="width: 70%"
                :disabled="reStartradio"
                class="restart-input"
                v-model="reStartForm.addrId"
                controls-position="right"
                :min="reStartradio ? 0 : 1"
                :step="1"
                :precision="0"
                placeholder="请输入重启地址"
            ></el-input-number>
        </el-form-item>
        <div class="restart-btn">
            <el-button
                type="primary"
                size="small"
                @click="handleRestart"
                :loading="restartBtnLoading"
                >重启</el-button
            >
        </div>
    </el-form>
</template>
<script>
import { handleConfirm } from "@/utils/messageBox";
import { mapGetters } from "vuex";
import { equipment } from "@/api/factuer";

export default {
    props: {
        currentRow: {
            type: Object,
            default: () => {},
        },
        restartVisible: {
            type: Boolean,
            default: false,
        },
    },
    data() {
        return {
            reStartForm: {
                value: "",
                addrId: 1,
            },
            reStartFormRules: {
                value: [
                    {
                        required: true,
                        message: "请选择重启设置",
                        trigger: "change",
                    },
                ],
                addrId: [
                    {
                        required: true,
                        message: "请输入重启地址",
                        trigger: "blur",
                    },
                ],
            },
            restartBtnLoading: false,
            reStartradio: false,
        };
    },
    computed: {
        ...mapGetters(["enums", "equipTypeCode"]),
    },
    watch: {
        restartVisible: {
            handler(val) {
                this.resetData();
            },
            immediate: true,
        },
    },
    methods: {
        reStartradioChange(e) {
            if (e) this.reStartForm.addrId = 0;
        },
        handleRestart() {
            this.$refs["reStartForm"].validate((valid) => {
                if (valid) {
                    handleConfirm("确定要重启吗？", "warning", "提示")
                        .then((res) => {
                            this.doReStart();
                        })
                        .catch((err) => {});
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        doReStart() {
            this.restartBtnLoading = true;
            let params = { ...this.reStartForm };
            params.value = params.value.toString();
            equipment(
                `/control/restart/${this.currentRow.id}`,
                "post",
                params,
                1
            )
                .then((res) => {
                    if (res.code == 200) {
                        this.$message.success(res.message);
                        this.$emit("cancelStartDialog");
                    }
                })
                .finally(() => (this.restartBtnLoading = false));
        },
        resetData() {
            this.reStartForm = {
                value: "",
                addrId: 1,
            };
            this.reStartradio = false;
            this.restartBtnLoading = false;
            this.$nextTick(() => {
                this.$refs["reStartForm"].resetFields();
            });
        },
    },
};
</script>
<style lang="scss" scoped>
.reStartForm {
    margin-top: 20px;

    .restart-select {
        margin-left: 70px;
        width: 70%;
    }

    .restart-input {
        margin-left: 20px;
        width: 202px;
    }
    ::v-deep .el-form-item__error {
        margin-left: 70px;
    }
}
.restart-btn {
    text-align: center;
}
</style>
